package com.csxy.qiu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.qiu.entity.EduSubject;
import com.csxy.qiu.entity.excel.SubjectData;
import com.csxy.qiu.entity.vo.OneSubject;
import com.csxy.qiu.entity.vo.TwoSubject;
import com.csxy.qiu.exception.MyException;
import com.csxy.qiu.listener.SubjectExcelListener;
import com.csxy.qiu.mapper.EduSubjectMapper;
import com.csxy.qiu.service.EduSubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询所有一级分类 parent_id = 0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> oneSubjectsList = baseMapper.selectList(wrapperOne);

        //查询所有二级分类 parent_id != 0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", "0");
        List<EduSubject> twoSubjectsList = baseMapper.selectList(wrapperTwo);

        //创建list集合，用于最终数据封装
        List<OneSubject> finalSubjectList = new ArrayList<>();
        //封装一级分类
        //查询出来所有的一级分类list集合遍历，获取每个一级分类对象，获取每个一级分类对象值，
        // 封装到要求的最终list集合中
        for (int i = 0; i < oneSubjectsList.size(); i++) {  //遍历oneSubjects集合，
            // 得到oneSubjectsList中每个eduSubject对象
            EduSubject oSubject = oneSubjectsList.get(i);
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(oSubject, oneSubject);
            finalSubjectList.add(oneSubject);

            //在一级分类循环遍历查询所有二级分类
            // 创建list集合封装每一个一级分类的二级分类
            ArrayList<TwoSubject> finalTwoSubjects = new ArrayList<>();

            //遍历查询所有的二级分类list集合
            for (int j = 0; j < twoSubjectsList.size(); j++) {
                //获取每个二级分类
                EduSubject tSubject = twoSubjectsList.get(j);
                //判断二级分类parent_id和一级分类id是否相同
                if (tSubject.getParentId().equals(oSubject.getId())) {
                    //把tSubject里面值获取出来，放到TwoSubject对象里面
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject, twoSubject);
                    //多个TwoSubject放到finalTwoSubjectList里面
                    finalTwoSubjects.add(twoSubject);
                }
            }

            //把一级下面的所有二级分类放到一级分类里面
            oneSubject.setChildren(finalTwoSubjects);
        }
        return finalSubjectList;
    }

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            //文件输入流
            InputStream is = file.getInputStream();
            EasyExcel.read(is, SubjectData.class,(ReadListener) new SubjectExcelListener(eduSubjectService)).sheet().doRead();

        } catch (Exception e) {
           throw new MyException(20001,"excel文件读取异常");
        }
    }
}
